
package com.hlkj.pay.controller.api;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.service.order.ICashierService;
import com.hlkj.pay.vo.openapi.order.resp.PayOrderSubmitApiResp;
import com.hlkj.pay.vo.order.req.*;
import com.hlkj.pay.vo.order.resp.CashierMchInfoResp;
import com.hlkj.pay.vo.order.resp.PayOrderResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "收银台")
@RestController
@RequestMapping("/api/cashier")
@RequiredArgsConstructor
@Slf4j
public class CashierPayController {

    private final ICashierService cashierService;

    @Operation(summary = "收银台下单")
    @PostMapping("/create")
    CommonResult<PayOrderSubmitApiResp> createOrder(@Validated @RequestBody CashierPayParamReq cashierPayParamReq) {
        return cashierService.createOrder(cashierPayParamReq);
    }

    @Operation(summary = "提交订单")
    @GetMapping("/submit/{order_no}/{auth_code}")
    CommonResult<PayOrderResp> submitOrder(@PathVariable("order_no") String orderNo, @PathVariable("auth_code") String authCode) {
        return cashierService.submitOrder(orderNo, authCode);
    }

    @Operation(summary = "获取商户详情")
    @PostMapping("/info")
    CommonResult<CashierMchInfoResp> getInfo(@RequestBody CashierPayParamReq cashierPayParamReq) {
        if (!StringUtils.hasText(cashierPayParamReq.getAppToken())) {
            return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
        }
        return cashierService.getInfo(cashierPayParamReq.getAppToken());
    }

    @Operation(summary = "获取微信授权链接")
    @GetMapping(value = "/wechat/auth/{order_no}")
    public CommonResult<String> wechatAuth(@PathVariable("order_no") String orderNo, @RequestParam("redirectUrl") String redirectUrl) {
        return cashierService.wechatAuth(orderNo, redirectUrl);
    }

    @Operation(summary = "获取支付宝授权链接")
    @GetMapping(value = "/alipay/auth/{order_no}")
    public CommonResult<String> alipayAuth(@PathVariable("order_no") String orderNo, @RequestParam("redirectUrl") String redirectUrl) {
        return cashierService.alipayAuth(orderNo, redirectUrl);
    }

}
